package com.udaos.modules.card.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.adaptor.WhaleAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.card.Ud_card_template;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.card.services.UdCardTemplateService;

@IocBean
@At("/platform/ud/coupon")
@Filters({ @By(type = PrivateFilter.class) })
public class UdCardTemplateController {

	@Inject
	private UdCardTemplateService couponModelService;

	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/card/coupon/model/index.html")
	@RequiresAuthentication
	public void index() {

	}

	/**
	 * 加载数据
	 * 
	 * @param coupon_name
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("coupon_name") String coupon_name, @Param("length") int length, @Param("start") int start,
			@Param("draw") int draw, @Param("::order") List<DataTableOrder> order,
			@Param("::columns") List<DataTableColumn> columns) {
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			Cnd cnd = Cnd.NEW();
			//获取主体
			Sys_user user = (Sys_user) subject.getPrincipal();
			String company_id=user.getUnit().getCompany_id();
			cnd.and("company_id", "=", company_id);
			if (!Strings.isBlank(coupon_name))
				cnd.and("coupon_name", "like", "%" + coupon_name + "%");
			return couponModelService.data(length, start, draw, order, columns, cnd, null);
		} else {
			return Result.error("数据加载失败");
		}
	}

	/**
	 * 新增卡券
	 * 
	 * @param unitid
	 * @return
	 */
	@At
	@Ok("beetl:/platform/card/coupon/model/add.html")
	@RequiresAuthentication
	public void add() {
	}

	/**
	 * 新增
	 * 
	 * @param coupon_model
	 * @param coupon_begin_timestamp_str
	 * @param coupon_end_timestamp_str
	 * @param request
	 * @return
	 */
	@At("/addDo")
	@Ok("json")
	@RequiresAuthentication
	@AdaptBy(type = WhaleAdaptor.class)
	public Object addDo(@Param("..") Ud_card_template coupon_model,
			@Param("coupon_begin_timestamp_str") String coupon_begin_timestamp_str,
			@Param("coupon_end_timestamp_str") String coupon_end_timestamp_str, HttpServletRequest request) {
		try {
			couponModelService.save(coupon_model, coupon_begin_timestamp_str, coupon_end_timestamp_str);
			return Result.success("保存成功");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @param req
	 * @return
	 */
	@At("/delete/?")
	@Ok("json")
	@RequiresAuthentication
	public Object delete(String id, HttpServletRequest req) {
		try {
			couponModelService.delete(id);
			return Result.success("删除成功");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}

	/**
	 * 详情
	 * 
	 * @param id
	 * @return
	 */
	@At("/detail/?")
	@Ok("beetl:/platform/card/coupon/model/detail.html")
	@RequiresAuthentication
	public Object detail(String id) {
		if (!Strings.isBlank(id)) {
			Ud_card_template model = couponModelService.fetch(id);
			return model;
		}
		return null;
	}

	/**
	 * 編輯
	 * 
	 * @param id
	 * @return
	 */
	@At("/edit/?")
	@Ok("beetl:/platform/card/coupon/model/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		if (!Strings.isBlank(id)) {
			Ud_card_template model = couponModelService.fetch(id);
			return model;
		}
		return null;
	}

	/**
	 * 编辑
	 * 
	 * @param coupon_model
	 * @param coupon_begin_timestamp_str
	 * @param coupon_end_timestamp_str
	 * @param request
	 * @return
	 */
	@At("/editDo")
	@Ok("json")
	@RequiresAuthentication
	@AdaptBy(type = WhaleAdaptor.class)
	public Object editDo(@Param("..") Ud_card_template coupon_model,
			@Param("coupon_begin_timestamp_str") String coupon_begin_timestamp_str,
			@Param("coupon_end_timestamp_str") String coupon_end_timestamp_str, HttpServletRequest request) {
		try {
			couponModelService.updateCoupon(coupon_model, coupon_begin_timestamp_str, coupon_end_timestamp_str);
			return Result.success("保存成功");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}

}
